<?php

namespace App\Service\UserCenter;

use App\Model\UserFollow;
use App\Model\UserProfile;
use App\Model\UserVisit;
use App\Service\BaseService;

/**
 * 访客列表
 */
class VisitListService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $page = $this->request->input('page', 1);
        $userIds = UserVisit::getList($userId);
        $ret['total'] = count($userIds);
        $start = ($page - 1) * $this->pageNum;
        $userIds = array_slice($userIds, $start, $this->pageNum);
        $usersInfo = UserProfile::getUsersProfile($userIds, false, true);
        $ret['list'] = [];
        //我的关注用户ID
        $followUserIds = UserFollow::getFollowList($userId);
        array_walk($usersInfo, function ($userProfile) use (&$ret, $followUserIds) {
            $item = UserProfile::buildUserItem($userProfile);
            $item['is_followed'] = in_array($item['user_id'], $followUserIds) ? 1 : 0;
            $ret['list'][] = $item;
        });
        return $ret;
    }
}
